package com.enqualcomm.kids.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.base.MyHandler;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;
import com.enqualcomm.kids.view.dialog.ConfirmShortNumberDialog;
import com.enqualcomm.kids.view.dialog.ConfirmShortNumberDialogCallback;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import common.utils.MyLogger;
import common.utils.PromptUtil;

/**
 * Created by Administrator on 2016/6/12.
 */
@EActivity(R.layout.activity_fill_watch_number)
public class FillWatchPhoneNumberActivity extends BaseActivity {
    @ViewById(R.id.single_sim_rl)
    RelativeLayout single_sim_rl;
    @ViewById(R.id.two_sim_rl)
    RelativeLayout two_sim_rl;
    @ViewById(R.id.single_iv)
    ImageView single_iv;
    @ViewById(R.id.two_iv)
    ImageView two_iv;
    @ViewById(R.id.country_regin_tv_1)
    TextView country_regin_tv_1;
    @ViewById(R.id.country_regin_tv_2)
    TextView country_regin_tv_2;
    @ViewById(R.id.phone_number_et_1)
    EditText phone_number_et_1;
    @ViewById(R.id.phone_number_et_2)
    EditText phone_number_et_2;
    @ViewById(R.id.input_number_ll_1)
    LinearLayout input_number_ll_1;
    @ViewById(R.id.input_number_ll_2)
    LinearLayout input_number_ll_2;
    @ViewById(R.id.title_bar_terminal_name_tv)
    TextView title_bar_terminal_name_tv;
//    @ViewById(R.id.title_bar_right_tv)
//    TextView title_bar_right_tv;

    private boolean isSingleSIM = true;
    TerminallistResult.Terminal terminal;

    @AfterViews
    void setupViews() {

        Intent intent = getIntent();
        terminal = intent.getParcelableExtra("terminal");
        String number1 = intent.getStringExtra("number1");
        String number2 = intent.getStringExtra("number2");
        String countryCode1 = intent.getStringExtra("countryCode1");
        String countryCode2 = intent.getStringExtra("countryCode2");
        int type = intent.getIntExtra("type", 0);

        if (!"".equals(countryCode1) && countryCode1 != null) {
            if (countryCode1.contains("+")) {
                countryCode1 = countryCode1.replace("+", "");
            }
            country_regin_tv_1.setText("+" + countryCode1);
        }
        if (!"".equals(countryCode2) && countryCode2 != null) {
            if (countryCode2.contains("+")) {
                countryCode2 = countryCode2.replace("+", "");
            }
            country_regin_tv_2.setText("+" + countryCode2);
        }
        if (!"".equals(number2) && number2 != null) {
            phone_number_et_2.setText(number2);
            phone_number_et_2.setSelection(number2.length());
        }
        if (!"".equals(number1) && number1 != null) {
            phone_number_et_1.setText(number1);
            phone_number_et_1.setSelection(number1.length());
        }
        if (type == 0) {
            two_iv.setImageResource(R.drawable.mode_no_select);
            single_iv.setImageResource(R.drawable.mode_select);
            input_number_ll_2.setVisibility(View.INVISIBLE);
            phone_number_et_1.setHint(R.string.fill_watch_input_sim_1);
            isSingleSIM = true;
        } else {
            phone_number_et_1.setHint(R.string.fill_watch_input_sim_2);
            phone_number_et_2.setHint(R.string.fill_watch_input_sim_3);
            input_number_ll_2.setVisibility(View.VISIBLE);
            two_iv.setImageResource(R.drawable.mode_select);
            single_iv.setImageResource(R.drawable.mode_no_select);
            isSingleSIM = false;
        }
        UserTerminalDefault userTerminalDefault = new UserTerminalDefault(terminal.userterminalid);
        setupTitleIcon(userTerminalDefault.getInfo(), terminal.terminalid, terminal.userterminalid, getString(R.string.f_p_watch_phone_number));
    }

    @Click({R.id.title_bar_title_tv, R.id.title_bar_left_iv})
    void goback() {
        finish();
    }

    @Click(R.id.country_regin_tv_1)
    void selectCountryone() {
        Intent intent = new Intent(this, ChooseCountryActivity.class);
        intent.putExtra("fromphonenumber", "fromphonenumber");
        startActivityForResult(intent, 10011);
    }

    @Click(R.id.country_regin_tv_2)
    void selectCountrytwo() {
        Intent intent = new Intent(this, ChooseCountryActivity.class);
        intent.putExtra("fromphonenumber", "fromphonenumber");
        startActivityForResult(intent, 10012);
    }

    @Click(R.id.single_sim_rl)
    void inputSingleNumber() {
        input_number_ll_2.setVisibility(View.INVISIBLE);
        two_iv.setImageResource(R.drawable.mode_no_select);
        single_iv.setImageResource(R.drawable.mode_select);
        phone_number_et_1.setHint(R.string.fill_watch_input_sim_1);
        country_regin_tv_2.setText("+86");
        phone_number_et_2.setText("");
        isSingleSIM = true;
    }

    @Click(R.id.two_sim_rl)
    void inputTwoNumber() {
        input_number_ll_2.setVisibility(View.VISIBLE);
        two_iv.setImageResource(R.drawable.mode_select);
        single_iv.setImageResource(R.drawable.mode_no_select);
        phone_number_et_1.setHint(R.string.fill_watch_input_sim_2);
        phone_number_et_2.setHint(R.string.fill_watch_input_sim_3);
        isSingleSIM = false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (10001 != resultCode) {
            return;
        }
        String countryCode = data.getStringExtra("number");
        switch (requestCode) {
            case 10011:
                country_regin_tv_1.setText(countryCode);
                break;
            case 10012:
                country_regin_tv_2.setText(countryCode);
                break;
        }
    }

    String type = "0";
    String number1;
    String number2;
    String countryCode1;
    String countryCode2;

    @Click(R.id.title_bar_right_tv)
    void saveNumber() {
        type = "0";
        number1 = phone_number_et_1.getText().toString().trim();
        number2 = phone_number_et_2.getText().toString().trim();
        countryCode1 = country_regin_tv_1.getText().toString().trim();
        countryCode2 = country_regin_tv_2.getText().toString().trim();
        if (isSingleSIM) {
            saveSingleNumber();
        } else {
            saveTwoNumber();
        }
    }

    private void saveInformation() {
        if (isSingleSIM) {
            if ((number1.length() <= 6 && !countryCode1.equals(getString(R.string.watch_numer_country_regin))) ||
                    (number1.length() > 6)) {           //手表号码是短号并且有国家代码或者手机号码大于6位
                countryCode1 = countryCode1.substring(1, countryCode1.length());
            } else {
                countryCode1 = "";
            }
            countryCode2 = "";
            number2 = "";
            type = "0";
        } else {
            if (number1.length() > 6) {
                countryCode1 = countryCode1.substring(1, countryCode1.length());
            } else if (number1.length() <= 6 && !countryCode1.equals(getString(R.string.watch_numer_country_regin))) {
                countryCode1 = countryCode1.substring(1, countryCode1.length());
            } else {
                countryCode1 = "";
            }

            if (number2.length() > 6) {
                countryCode2 = countryCode2.substring(1, countryCode2.length());
            } else if (number2.length() <= 6 && !countryCode2.equals(getString(R.string.watch_numer_country_regin))) {
                countryCode2 = countryCode2.substring(1, countryCode2.length());
            } else {
                countryCode2 = "";
            }
            type = "1";
        }
        Intent intent = new Intent();
        intent.putExtra("number1", number1);
        intent.putExtra("number2", number2);
        intent.putExtra("countryCode1", countryCode1);
        intent.putExtra("type", type);
        intent.putExtra("countryCode2", countryCode2);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void saveTwoNumber() {
        if ("".equals(number1) || "".equals(number2)) {
            //输入不能为空
            PromptUtil.toast(getApplicationContext(), R.string.input_empty);
            return;
        }

        if ((countryCode1.equals(getString(R.string.watch_numer_country_regin)) && (number1.length()) > 6)
                && (countryCode2.equals(getString(R.string.watch_numer_country_regin)) && (number2.length()) > 6)) {
            //号码格式有误
            PromptUtil.toast(getApplicationContext(), R.string.country_code_empty);
            return;
        } else if (countryCode1.equals(getString(R.string.watch_numer_country_regin)) && (number1.length()) > 6) {
            PromptUtil.toast(getApplicationContext(), getString(R.string.pre_select_phone_number) + "1" + getString(R.string.country_code_empty));
            return;
        } else if (countryCode2.equals(getString(R.string.watch_numer_country_regin)) && (number2.length()) > 6) {
            PromptUtil.toast(getApplicationContext(), getString(R.string.pre_select_phone_number) + "2" + getString(R.string.country_code_empty));
            return;
        }

        if ((number1.length() <= 6 && !countryCode1.equals(getString(R.string.watch_numer_country_regin)))
                && (number2.length() <= 6 && !countryCode2.equals(getString(R.string.watch_numer_country_regin)))) {
            showdialog(3);
        } else if (number1.length() <= 6 && !countryCode1.equals(getString(R.string.watch_numer_country_regin))) {
            showdialog(1);
        } else if (number2.length() <= 6 && !countryCode2.equals(getString(R.string.watch_numer_country_regin))) {
            showdialog(2);
        } else {
            saveInformation();
        }

    }

    private void saveSingleNumber() {
        if ("".equals(number1)) {
            //输入不能为空
            PromptUtil.toast(getApplicationContext(), R.string.input_empty);

        } else if (countryCode1.equals(getString(R.string.watch_numer_country_regin)) && (number1.length()) > 6) {
            //号码格式有误
            PromptUtil.toast(getApplicationContext(), R.string.country_code_empty);
        } else {
            //输入正确
            if (number1.length() <= 6 && !countryCode1.equals(getString(R.string.watch_numer_country_regin))) {
                showdialog(3);
            } else {
                saveInformation();
            }
        }
    }

    private void showdialog(int phoneNum) {
        new ConfirmShortNumberDialog(this, phoneNum, new ConfirmShortNumberDialogCallback() {
            @Override
            public void save() {
                saveInformation();
            }

            @Override
            public void cancle(int number) {
                if ((isSingleSIM && number == 3) || (number == 1)) {
                    Intent intent = new Intent(FillWatchPhoneNumberActivity.this, ChooseCountryActivity.class);
                    intent.putExtra("fromphonenumber", "fromphonenumber");
                    startActivityForResult(intent, 10011);
                } else if (number == 2) {
                    Intent intent = new Intent(FillWatchPhoneNumberActivity.this, ChooseCountryActivity.class);
                    intent.putExtra("fromphonenumber", "fromphonenumber");
                    startActivityForResult(intent, 10012);
                }
            }
        }).show();
    }
}
